package com.bcu.wish.controller;

import com.bcu.wish.entity.Message;
import com.bcu.wish.entity.User;
import com.bcu.wish.entity.search.UserSearch;
import com.bcu.wish.service.TimeService;
import com.bcu.wish.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@Api(tags = "用户模块")
@RestController
@RequestMapping("/api/user")
public class UserController {
    @Resource
    private UserService userService;

    @Resource
    private TimeService timeService;

    @ApiOperation(value = "小程序端登入")
    @PostMapping(value = "/login")
    public Message login(String userId, String userPwd) {
        return userService.login(userId,userPwd);
    }

    @ApiOperation(value = "管理端登录")
    @PostMapping(value = "/admin/login")
    public Message adminLogin(String userId,String userPwd){
        return userService.adminLogin(userId, userPwd);
    }

    @ApiOperation(value = "查询用户录取结果")
    @GetMapping(value = "/result")
    public Message getResult(String userId) {
        Date begindate = timeService.selectByPrimaryKey(9).getObj().getTimeLimittime(); //开始查看录取结果时间
        Date stopdate = timeService.selectByPrimaryKey(10).getObj().getTimeLimittime();  //截止查看录取结果时间
        Date nowdate = new Date();
        if(nowdate.after(begindate)) {
            if(nowdate.before(stopdate)) {
                return userService.selectUserResultByUserId(userId);
            }
            return Message.fail("查看录取结果时间已截止！");
        }
        return Message.fail("查看录取结果时间未开始！");
    }

    @ApiOperation(value = "根据userId查看用户信息")
    @GetMapping(value = "/info")
    public Message getUserinfo(String userId) {
        return userService.selectByPrimaryKey(userId);
    }

    @ApiOperation(value = "查看所有user")
    @GetMapping(value = "/")
    public Message getAllUser() {
        return userService.selectAll();
    }

    @ApiOperation(value = "注销")
    @PostMapping(value = "/logout")
    public Message logout()
    {
        return Message.success("注销成功");
    }

    @ApiOperation(value = "根据用户类型查找用户")
    @GetMapping(value = "/usertype")
    public Message getAllStudent(Integer userType) {
        return userService.selectByUserType(userType);
    }

    @ApiOperation(value = "新增管理员用户")
    @PostMapping(value = "/add")
    public Message addAdmin(User user) {
        return userService.insert(user);
    }

    @ApiOperation(value = "导出录取结果Excel,返回url")
    @GetMapping(value = "/Excel")
    public Message getExcel(){
        return userService.getExcel();
    }

    @ApiOperation(value = "根据id更新录取结果")
    @PutMapping(value = "/result")
    public Message updateUserResultByUserId(String userId, String userResult) {
        return userService.updateUserResultByUserId(userId,userResult);
    }

    @ApiOperation(value = "重置密码")
    @PostMapping(value = "/reset")
    public Message resetPassword(String userId) {
        return userService.resetPassword(userId);
    }

    @ApiOperation(value = "修改密码")
    @PutMapping(value = "/pwd")
    public Message updatePwd(String userId, String userPwd) {
        return userService.changePwd(userId, userPwd);
    }

    @ApiOperation(value = "模糊条件搜索")
    @GetMapping(value = "/search")
    public Message search(UserSearch user){
        return userService.search(user);
    }

    @ApiOperation(value = "导出完整录取结果Excel,返回url")
    @GetMapping(value = "/Excel2")
    public Message getExcel2(){
        return userService.getExcel2();
    }
}
